package cn.tips.dmp.supports.global.handler;

import lombok.extern.slf4j.Slf4j;
import cn.tips.dmp.supports.result.ApiResultDTO;
import cn.tips.dmp.supports.result.ApiResultGenerator;
import cn.tips.dmp.supports.status.ApiStatusCode;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义常用http错误状态处理Controller<br>
 * 拦截401,404,500错误<br>
 * 并且实现html与json状态下的返回
 *
 * @author pkb
 */
@SuppressWarnings("unused")
@Controller
@RequestMapping("/error")
@Slf4j
public class CustomErrorPageController {

    /**
     * 定义400 bad_request 的ModelAndView
     *
     * @param request  请求
     * @param response 返回
     * @return error/400模板
     */
    @RequestMapping(produces = "text/html", value = "400")
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ModelAndView errorHtml400(HttpServletRequest request, HttpServletResponse response) {
        log.error("全局捕获(400:BAD_REQUEST)异常!");
        return new ModelAndView("error/400");
    }

    /**
     * 定义400 bad_request 的JSON数据
     *
     * @param request 请求
     * @return json类型返回值
     */
    @RequestMapping(value = "400")
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ApiResultDTO error400(HttpServletRequest request) {
        log.error("全局捕获(400:BAD_REQUEST)异常!");
        return ApiResultGenerator.create(ApiStatusCode.BAD_REQUEST.getCode(), ApiStatusCode.BAD_REQUEST.getDescription());
    }

    /**
     * 定义401的ModelAndView
     *
     * @param request  请求
     * @param response 返回
     * @return error/401模板
     */
    @RequestMapping(produces = "text/html", value = "401")
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ModelAndView errorHtml401(HttpServletRequest request, HttpServletResponse response) {
        log.error("全局捕获(401:UNAUTHORIZED)异常!");
        return new ModelAndView("error/401");
    }

    /**
     * 定义401的JSON数据
     *
     * @param request 请求
     * @return json类型返回值
     */
    @RequestMapping(value = "401")
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public ApiResultDTO error401(HttpServletRequest request) {
        log.error("全局捕获(401:UNAUTHORIZED)异常!");
        return ApiResultGenerator.create(ApiStatusCode.UNAUTHORIZED.getCode(), ApiStatusCode.UNAUTHORIZED.getDescription());
    }

    /**
     * 定义404的ModelAndView
     *
     * @param request  请求
     * @param response 返回
     * @return error/404模板
     */
    @RequestMapping(produces = "text/html", value = "404")
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ModelAndView errorHtml404(HttpServletRequest request, HttpServletResponse response) {
        log.error("全局捕获(404:NOT_FOUND)异常!");
        return new ModelAndView("error/404");
    }

    /**
     * 定义404的JSON数据
     *
     * @param request 请求
     * @return json类型返回值
     */
    @RequestMapping(value = "404")
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ResponseBody
    public ApiResultDTO error404(HttpServletRequest request) {
        log.error("全局捕获(404:NOT_FOUND)异常!");
        return ApiResultGenerator.create(ApiStatusCode.NOT_FOUND.getCode(), ApiStatusCode.NOT_FOUND.getDescription());
    }

    /**
     * 定义405的ModelAndView
     *
     * @param request  请求
     * @param response 返回
     * @return error/405模板
     */
    @RequestMapping(produces = "text/html", value = "405")
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    public ModelAndView errorHtml405(HttpServletRequest request, HttpServletResponse response) {
        log.error("全局捕获(405:METHOD_NOT_ALLOWED)异常!");
        return new ModelAndView("error/405");
    }

    /**
     * 定义405的JSON数据
     *
     * @param request 请求
     * @return json类型返回值
     */
    @RequestMapping(value = "405")
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ResponseBody
    public ApiResultDTO error405(HttpServletRequest request) {
        log.error("全局捕获(405:METHOD_NOT_ALLOWED)异常!");
        return ApiResultGenerator.create(ApiStatusCode.METHOD_NOT_ALLOWED.getCode(), ApiStatusCode.METHOD_NOT_ALLOWED.getDescription());
    }

    /**
     * 定义500的ModelAndView
     *
     * @param request  请求
     * @param response 返回
     * @return error/500模板
     */
    @RequestMapping(produces = "text/html", value = "500")
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ModelAndView errorHtml500(HttpServletRequest request, HttpServletResponse response) {
        log.error("全局捕获(500:INTERNAL_SERVER_ERROR)异常!");
        return new ModelAndView("error/500");
    }

    /**
     * 定义500的JSON数据
     *
     * @param request 请求
     * @return json类型返回值
     */
    @RequestMapping(value = "500")
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ApiResultDTO error500(HttpServletRequest request) {
        log.error("全局捕获(500:INTERNAL_SERVER_ERROR)异常!");
        return ApiResultGenerator.create(ApiStatusCode.ERROR.getCode(), ApiStatusCode.ERROR.getDescription());
    }
}
